package com.edu.dao;

import com.edu.entity.FoodType;
import com.edu.utils.JdbcHelper;
import com.edu.utils.PageList;
import com.edu.utils.PageParam;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author ZCN
 * @Date 2021/11/12 16:31
 * @Version 1.0
 **/
public class FoodTypeDao {
    // 分页数据
    public PageList pageList(PageParam pageParam) throws SQLException {
        String countsql = "select count(*) from foodtype";
        String pagesql ="select * from foodtype limit ?,?";
        Long count = (Long) JdbcHelper.getSingle(countsql);
        List data = JdbcHelper.query(pagesql, FoodType.class, pageParam.getStart(),pageParam.getLimit());
        return new PageList(count,data);
    }

    // 查询所有
    public List<Object> findAll() throws SQLException {
        String sql = "select * from foodtype";
        List<Object> list = JdbcHelper.query(sql,FoodType.class);
        return list;
    }

    // 根据id查询
    public FoodType findOne(Integer id) throws SQLException {
        String sql = "select * from foodtype where id = ?";
        List<FoodType> list = JdbcHelper.query(sql, FoodType.class, id);
        return list.get(0);
    }

    // 修改
    public void update(FoodType foodType) throws SQLException {
        String sql = "update foodtype set typename = ? where id = ?";
        JdbcHelper.update(sql, foodType.getTypename(), foodType.getId());
    }

    // 删除
    public void delete(Integer id) throws SQLException {
        String sql = "delete from foodtype where id = ?";
        JdbcHelper.update(sql,id);
    }

    // 添加
    public void insert(FoodType foodType) throws SQLException {
        String sql = "insert into foodtype(typename) values(?)";
        JdbcHelper.update(sql,foodType.getTypename());
    }
}
